//
//  LVSessionMorePopView.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/11.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVSessionMorePopView.h"
#import "HYAnimationManager.h"
#import "UIView+Layer.h"
#import "FXBlurView.h"
#import "NSString+Size.h"

#define kTag 333333

@interface LVSessionPopCell : UITableViewCell
@property (strong,nonatomic) UIButton *titleBtn;
@end
@implementation LVSessionPopCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_titleBtn setTitleColor:KMainColor forState:UIControlStateNormal];
        _titleBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleBtn.userInteractionEnabled = NO;
        [self.contentView addSubview:_titleBtn];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleBtn.left = 40;
    _titleBtn.top = 5;
    _titleBtn.width = self.width - 80;
    _titleBtn.height = self.height - 10;

    [_titleBtn makeRoundedCorners];
    [_titleBtn makeBorderColor:KMainColor borderWidth:1.5];
}
@end
@interface LVSessionMorePopView ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic)FXBlurView *blurView;
@property (strong, nonatomic)UIView *contentView;
@property (strong, nonatomic)UILabel *alertTitleLabel;
@property (strong, nonatomic)UILabel *numberabel;
@property (strong, nonatomic) UIView *line;

@property (nonatomic, strong) NSMutableArray *btnTitlesArray;

@property (nonatomic, copy)   NSString *cancelTitle;
/**
 *  内容视图
 */
@property (nonatomic, weak)   UITableView *tableView;

@property (nonatomic, copy) NSString *alertTitle;
@property (nonatomic, copy) NSString *subTitle;

@property (copy, nonatomic) BOOL (^cancleBlock)();
@end
@implementation LVSessionMorePopView
{
    LVSessionMorePopViewItemClickHandle _clickHandle;
}
+ (BOOL)alertIsShowingInView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    UIView *alert = [view viewWithTag:kTag];
    return alert != nil;
}
+ (instancetype)actionMorePopViewWithCancelTitle:(NSString *)cancelTitle alertTitle:(NSString *)alertTitle SubTitle:(NSString *)subTitle btnTitles:(NSString *)title,...NS_REQUIRES_NIL_TERMINATION
{
    LVSessionMorePopView *sheet = [[LVSessionMorePopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    sheet.cancelTitle = cancelTitle;
    sheet.alertTitle = alertTitle;
    sheet.subTitle = subTitle;

    NSString *str;
    va_list argumentList;
    if(title) {
        [sheet.btnTitlesArray addObject:title];
        va_start(argumentList, title);
        while((str = va_arg(argumentList, id))) {
            NSString *tit = [str copy];
            [sheet.btnTitlesArray addObject:tit];
        }
        va_end(argumentList);
    }
    return sheet;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}
- (void)setUp
{
    _alertTitleLabel = [[UILabel alloc] init];
    _alertTitleLabel.textColor = [UIColor blackColor];
    _alertTitleLabel.textAlignment = NSTextAlignmentCenter;
    _alertTitleLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:_alertTitleLabel];

    _numberabel = [[UILabel alloc] init];
    _numberabel.textColor = KGrayColor;
    _numberabel.textAlignment = NSTextAlignmentCenter;
    _numberabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_numberabel];

    _line = [[UIView alloc] init];
    _line.backgroundColor = KLightGrayColor;
    [self.contentView addSubview:_line];

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];

    _blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _blurView.tintColor = [UIColor clearColor];

    [self.tableView reloadData];
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    _alertTitleLabel.text = self.alertTitle;
    _numberabel.text = self.subTitle;

    self.contentView.frame = CGRectMake(0, 0, kScreenWidth - 80, self.btnTitlesArray.count * 50 + 115);
    self.contentView.center = self.center;
    
    self.tableView.frame = CGRectMake(0, 82, kScreenWidth - 80, self.btnTitlesArray.count * 50 + 33);

    _alertTitleLabel.width = self.contentView.width - 80;
    _alertTitleLabel.height = 25;
    _alertTitleLabel.centerX = self.contentView.width / 2;
    _alertTitleLabel.top = 10;

    _line.width = [_alertTitle widthWithFont:[UIFont systemFontOfSize:18] constrainedToHeight:25] + 10;
    _line.height = 0.8;
    _line.centerX = _alertTitleLabel.centerX;
    _line.top = _alertTitleLabel.bottom + 10;

    _numberabel.width = self.contentView.width - 80;
    _numberabel.height = 18;
    _numberabel.centerX = _line.centerX;
    _numberabel.top = _line.bottom + 10;
}
- (void)setCustomActionPopViewItemClickHandle:(LVSessionMorePopViewItemClickHandle)itemClickHandle
{
    _clickHandle = itemClickHandle;
}
- (void)setupCancelBlock:(BOOL (^)())cancelBlock
{
    _cancleBlock = cancelBlock;
}
- (void)cancelAction:(UIButton *)btn
{
    if (_cancleBlock) {
        _cancleBlock();
    }
    [self dismissFromSuperview];
}
#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.btnTitlesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LVSessionPopCell *popCell = [tableView dequeueReusableCellWithIdentifier:@"LVSessionPopCell"];
    if (!popCell) {
        popCell = [[LVSessionPopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LVSessionPopCell"];
    }
    NSString *title = self.btnTitlesArray[indexPath.row];
    [popCell.titleBtn setTitle:title forState:UIControlStateNormal];
    return popCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_clickHandle) {
        NSString *title = self.btnTitlesArray[indexPath.row];
        _clickHandle(self, indexPath.row, title);
    }
    [self dismissFromSuperview];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 33;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIButton *footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    footBtn.frame = CGRectMake(0, 0, kScreenWidth - 80, 33);
    [footBtn setTitle:_cancelTitle forState:UIControlStateNormal];
    [footBtn setTitleColor:KGrayColor forState:UIControlStateNormal];
    [footBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    footBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];

    return footBtn;
}
#pragma mark 懒加载
- (UIView *)contentView {
    if (_contentView == nil) {
        UIView *contentView = [[UIView alloc] init];
        [self addSubview:contentView];
        _contentView = contentView;
        contentView.layer.cornerRadius = 13.f;
        contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}
- (UITableView *)tableView {
    if(!_tableView) {
        UITableView *tab = [[UITableView alloc] init];
        tab.delegate = self;
        tab.dataSource = self;
        tab.backgroundColor = [UIColor clearColor];
        tab.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:tab];
        tab.scrollEnabled = NO;
        _tableView = tab;
    }
    return _tableView;
}
- (NSMutableArray *)btnTitlesArray {
    if(!_btnTitlesArray) {
        _btnTitlesArray = [[NSMutableArray alloc] init];
    }
    return _btnTitlesArray;
}
#pragma mark show/dismiss
- (void)showInView:(UIView *)view {

    BOOL isShowing = [LVSessionMorePopView alertIsShowingInView:view];
    if (isShowing) return ;

    UIView *content = view ? view  : [UIApplication sharedApplication].keyWindow;
    _blurView.blurRadius = 0;
    self.frame = content.bounds;
    self.alpha = 0;
    self.tag = kTag;

    [content addSubview:_blurView];
    [content addSubview:self];

    [HYAnimationManager addTransformAnimationForView:self.contentView];

    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 1;
        _blurView.blurRadius = 12.0f;
    } completion:^(BOOL finished) {
        _blurView.dynamic = NO;
        [FXBlurView setUpdatesDisabled];
    }];
}
//移除掉
- (void)dismissFromSuperview
{
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            [_blurView clearImage];
            [_blurView removeFromSuperview];
        }
    }];
}
@end
